package logger

import (
	"io"
	"log"
	"os"
)

var instance *Logger

type Logger struct {
	i *log.Logger
	w *log.Logger
	e *log.Logger
}

func GetLogger() *Logger {
	if instance == nil {
		instance = new(Logger)
	}
	return instance
}

func (l *Logger) Init() error {
	file, err := os.OpenFile("./logs/info.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)

	if file == nil || err != nil {
		log.Fatalln("打开日志文件失败：", err)
		return err
	}

	l.i = log.New(io.MultiWriter(os.Stderr, file), "【信息】 ", log.Ldate|log.Ltime)
	l.w = log.New(io.MultiWriter(os.Stderr, file), "【警告】 ", log.Ldate|log.Ltime)
	l.e = log.New(io.MultiWriter(os.Stderr, file), "【错误】 ", log.Ldate|log.Ltime)
	l.D("[Init] 日志初始化完成")
	return nil
}

func (l *Logger) D(s string, v ...interface{}) {
	l.i.Printf(s, v...)
}

func (l *Logger) W(s string, v ...interface{}) {
	l.w.Printf(s, v...)
}

func (l *Logger) E(s string, v ...interface{}) {
	l.e.Printf(s, v...)
}
